package com.zhanglinwei.openApiClient.utils;

import com.zhanglinwei.openApiClient.exception.OpenApiParseException;

import java.util.Locale;

public class PropertyUtils {

    private PropertyUtils(){}

    public static String methodToProperty(String methodName) {
        if (methodName.startsWith("is")) {
            methodName = methodName.substring(2);
        } else {
            if (!methodName.startsWith("get") && !methodName.startsWith("set")) {
                throw new OpenApiParseException("Error parsing property name '" + methodName + "'.  Didn't start with 'is', 'get' or 'set'.");
            }

            methodName = methodName.substring(3);
        }

        if (methodName.length() == 1 || methodName.length() > 1 && !Character.isUpperCase(methodName.charAt(1))) {
            methodName = methodName.substring(0, 1).toLowerCase(Locale.ENGLISH) + methodName.substring(1);
        }

        return methodName;
    }
}
